import { postLikeCase, postMarkCase, followSupplier } from '../../../api/index'
const app = getApp()

Component({
  properties: {
    item: {
      type: Object,
      value: {}
    }
  },
  data: {
    caseSwiperIndex: 0,
    ishidden: false,
    isMore: false,
    userInfo: {}
  },
  methods: {
    async collectionSupplier () {
      try {
        await followSupplier({ supplierId: this.data.item.supplierId })
        wx.showToast({ title: '操作成功！', icon: 'none' })
        this.data.item.followFlag = !this.data.item.followFlag
        this.setData({ 'item.followFlag': this.data.item.followFlag })
        this.triggerEvent('change', this.data.item)
      } catch (err) {
        wx.showToast({ title: err.message || err.ResultSet.errors, icon: 'none' })
      }
    },
    changeCaseSwiper ({ detail: { current } }) {
      this.setData({ caseSwiperIndex: current })
    },
    showCaseMore () {
      this.setData({ ishidden: !this.data.ishidden })
    },
    async giveLike () {
      try {
        await postLikeCase({ materialCaseId: this.data.item.materialCaseId })
        this.data.item.likeFlag = !this.data.item.likeFlag
        if (this.data.item.likeFlag) {
          this.data.item.likeCount = this.data.item.likeCount + 1
        } else {
          this.data.item.likeCount = this.data.item.likeCount - 1
        }
        wx.showToast({ title: '操作成功！', icon: 'none' })
        this.setData({ 'item.likeFlag': this.data.item.likeFlag, 'item.likeCount': this.data.item.likeCount })
        this.triggerEvent('change', this.data.item)
      } catch (err) {
        wx.showToast({ title: err.message || err.ResultSet.errors, icon: 'none' })
      }
    },
    async changeMark () {
      try {
        await postMarkCase({ materialCaseId: this.data.item.materialCaseId })
        this.data.item.markFlag = !this.data.item.markFlag
        if (this.data.item.markFlag) {
          this.data.item.markCount = this.data.item.markCount + 1
        } else {
          this.data.item.markCount = this.data.item.markCount - 1
        }
        wx.showToast({ title: '操作成功！', icon: 'none' })
        this.setData({ 'item.markFlag': this.data.item.markFlag, 'item.markCount': this.data.item.markCount })
        this.triggerEvent('change', this.data.item)
      } catch (err) {
        wx.showToast({ title: err.message || err.ResultSet.errors, icon: 'none' })
      }
    },
    seeDetail () {
      wx.$nav.navigateTo(`/pages/caseDetail/index?type=${this.data.item.type}&id=${this.data.item.materialCaseId}`)
    }
  },
  attached () {
    const userInfo = app.globalData.userInfo ? app.globalData.userInfo : {}
    this.setData({ userInfo: userInfo })
    let query = this.createSelectorQuery();
    query.select('.case-detail-desc').boundingClientRect(rect=>{
      let height = rect.height
      if (this.data.item.materialCaseId === 1190) console.log(height)
      if (height > 66) {
        this.setData({ ishidden: true, isMore: true })
      } else {
        this.setData({ isMore: false })
      }
    }).exec()
  }
})